<?php
class ControllerAffiliateRegister extends FrontController {
	
  	public function index() {
		if ($this->affiliate->isLogged()) {
	  		$this->redirect(US('affiliate/account'));
    	}

		$this->document->setTitle($this->_['heading_title']);

		M('affiliate/affiliate');

    	if ($this->request->isPost() && $this->validate()) {
			$this->model_affiliate_affiliate->addAffiliate($this->request->post);
			$this->affiliate->login($this->request->post['email'], $this->request->post['password']);
	  		$this->redirect(U('affiliate/success'));
    	}

      	$this->data['breadcrumbs'][] = array(
        	'text'      => $this->_['text_account'],
			'href'      => US('affiliate/account'),
        	'separator' => $this->_['text_separator']
      	);

      	$this->data['breadcrumbs'][] = array(
        	'text'      => $this->_['text_register'],
			'href'      => US('affiliate/register'),
        	'separator' => $this->_['text_separator']
      	);
		
		$this->document->addScript('static/javascript/jquery/jquery.validate.js');
		
		$this->data['text_account_already'] = sprintf($this->_['text_account_already'], U('affiliate/login', '', 'SSL'));

   		$this->data['firstname'] = P('firstname');
		$this->data['lastname'] = P('lastname');
		$this->data['email'] = P('email');
		$this->data['telephone'] = P('telephone');
		$this->data['fax'] = P('fax');
		$this->data['company'] = P('company');
		$this->data['website'] = P('website');
		$this->data['address_1'] = P('address_1');
		$this->data['address_2'] = P('address_2');
		$this->data['postcode'] = P('postcode');
		$this->data['city'] = P('city');
		$this->data['country_id'] = P('country_id', C('config_country_id'));
		$this->data['zone_id'] = P('zone_id');
		$this->data['tax'] = P('tax');
		$this->data['payment'] = P('payment', 'cheque');
		$this->data['cheque'] = P('cheque');
		$this->data['paypal'] = P('paypal');
		$this->data['bank_name'] = P('bank_name');
		$this->data['bank_branch_number'] = P('bank_branch_number');
		$this->data['bank_swift_code'] = P('bank_swift_code');
		$this->data['bank_account_name'] = P('bank_account_name');
		$this->data['bank_account_number'] = P('bank_account_number');
		$this->data['password'] = P('password');
		$this->data['confirm'] = P('confirm');
		
    	$this->data['countries'] = cache_read('country.php');

		if (C('config_affiliate_id')) {
			M('catalog/page');

			$page_info = $this->model_catalog_page->getPage(C('config_affiliate_id'));

			if ($page_info) {
				$this->data['text_agree'] = sprintf($this->_['text_agree'], U('page/index/info', 'page_id=' . C('config_affiliate_id'), 'SSL'), $page_info['title'], $page_info['title']);
			} else {
				$this->data['text_agree'] = '';
			}
		} else {
			$this->data['text_agree'] = '';
		}

		$this->data['agree'] = P('agree', false);

		$this->display('affiliate/register.tpl');
  	}

  	protected function validate() {
		if ($this->model_affiliate_affiliate->getTotalAffiliatesByEmail($this->request->post['email'])) {
      		$this->setMessage('error_warning', $this->_['error_exists']);
			return false;
    	}
		
		if (C('config_affiliate_id')) {
			M('catalog/page');
			$page_info = $this->model_catalog_page->getPage(C('config_affiliate_id'));
			
			if ($page_info && !isset($this->request->post['agree'])) {
      			$this->setMessage('error_warning', sprintf($this->_['error_agree'], $page_info['title']));
				return false;
			}
		}
		
		$this->load->library('form_validation', true);
		$this->form_validation->set_rules('firstname', '', 'required|range_length[1,32]', $this->_['error_firstname']);		
		$this->form_validation->set_rules('lastname', '', 'required|range_length[1,32]', $this->_['error_lastname']);
		
		$this->form_validation->set_rules('email', '', 'required|range_length[5,96]|email', $this->_['error_email']);
		
		$this->form_validation->set_rules('telephone', '', 'required|range_length[3,32]', $this->_['error_telephone']);
		
		$this->form_validation->set_rules('address_1', '', 'required|range_length[3,128]', $this->_['error_address_1']);
		
		$this->form_validation->set_rules('city', '', 'required|range_length[2,128]', $this->_['error_city']);
		
		
		M('localisation/country');
		$country_info = $this->model_localisation_country->getCountry($this->request->post['country_id']);

		if ($country_info && $country_info['postcode_required']) {
			$this->form_validation->set_rules('postcode', '', 'required|range_length[2,10]', $this->_['error_postcode']);	
		}
		
		$this->form_validation->set_rules('country_id', '', 'required', $this->_['error_country']);
		$this->form_validation->set_rules('zone_id', '', 'required', $this->_['error_zone']);

		$this->form_validation->set_rules('password', '', 'required|range_length[4,20]', $this->_['error_password']);
		$this->form_validation->set_rules('confirm', '', 'required|matches[password]', $this->_['error_confirm']);

    	return $this->form_validation->run();
  	}
}
?>